#! /usr/bin/env python
# coding=utf-8

# 对 mafft文件 运行 aliscore评分

import os
import threading

import sys
import argparse


parser = argparse.ArgumentParser(
    description=''' 对运行mafft比对运行 aliscore
    用法:
    run_aliscore.py -a /home/good/tools/Aliscore_v.2.0/Aliscore.02.2.pl -i Orthogroup_Sequences_mafft_raw -t 64
    由大天才于2021年7月19日创建于浙江农业大学''')



parser.add_argument('-a',
                help='必须给定，Aliscore.02.2.pl脚本的路径')



parser.add_argument('-i',
                help='必须给定，mafft比对结果的路径')

parser.add_argument('-t',
                help='并行核数 默认为8')

args = parser.parse_args()

if not args.i or not args.a:
    parser.print_help()
    sys.exit()




infile = args.i

alipath = args.a

if not args.t:
	thread = 8

thread = int(args.t)




index = 0



thread_pool = []



def run_func(file_path):

	global index
	global alipath

	index = index + 1

	a = os.system(alipath + ' -i %s' % file_path)

	if index >= len(thread_pool):
		return None
	else:
		thread_pool[index].start()

	return	None

n = 0

for i in os.listdir(infile):

	file_path = infile+'/'+i

	n += 1

	th = threading.Thread(target=run_func, args=[file_path], name='th_'+str(n))
	thread_pool.append(th)


for i in range(thread):
	if len(thread_pool)>i:
		thread_pool[i].start()

